<?php

class InstallForm extends CFormModel {
    
    const CONST_APP_NAME = 'MEME_APP_NAME';
    const CONST_ADMIN_EMAIL = 'ADMIN_EMAIL';
    const CONST_DB_HOST = 'DB_HOST';
    const CONST_DB_NAME = 'DB_NAME';
    const CONST_DB_USER = 'DB_USER';
    const CONST_DB_PASSWORD = 'DB_PASSWORD';

    public $app_name;
    public $admin_username;
    public $admin_password;
    public $admin_email;
    public $database_host;
    public $database_name;
    public $database_username;
    public $database_password;

    public function rules() {
        return array(
            array('app_name,admin_username,admin_password,admin_email,database_host,database_name,database_username', 'required'),
            array('admin_email', 'email'),
            array('database_password', 'safe'),
            array('database_host', 'test_db'),
        );
    }
    
    public function test_db() {
        if($this->database_host && $this->database_name) {
            try {
                $db = new CDbConnection("mysql:host={$this->database_host};dbname={$this->database_name}", $this->database_username, $this->database_password);
                @$db->active = true;
                Yii::app()->setComponent('db', $db);
                return TRUE;
            }
            catch (Exception $e) {
                $this->addError('database_host', 'Check your database details.');
                $this->addError('database_username', 'Check your database details.');
                $this->addError('database_password', 'Check your database details.');
            }
        }
    }

    public function attributeLabels() {
        return array(
            'app_name' => 'Application name',
            'admin_username' => 'Admin username',
            'admin_password' => 'Admin password',
            'admin_email' => 'Admin email',
            'database_host' => 'Database host',
            'database_name' => 'Database name',
            'database_username' => 'Database username',
            'database_password' => 'Database password',
        );
    }
}
